<template>
  <div>
    <h1>Content</h1>
    <p>请求数据{{ message }}</p>
    <button @click="cancel">解绑</button>
  </div>
</template>

<script>
import Pubsub from "pubsub-js";
export default {
  name: "Content",
  data() {
    return {
      message: "",
    };
  },
  //一般订阅都是初始化
  //Pubsub提供了subscribe方法用来订阅某个发布,一旦发布则会调用回调函数,回调函数第二个参数就是发布的值
  mounted() {
    Pubsub.subscribe("headerMessage", (name, message) => {
      console.log("headerMessage订阅收到了消息", name, message);
      this.message = message;
    });
  },
  methods: {
    cancel() {
      Pubsub.unsubscribe("headerMessage");
    },
  },
};
</script>

<style>
</style>